<?php

class Ead_ContatoprfController extends Ead_Controller_Action
{

    public function init()
    {
       
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ead/contato.js');

        $sessao = new Zend_Session_Namespace('Professor');
        $this->view->professor = $sessao->professor;
        
        $query = 'SELECT SQL_CACHE * FROM CURSO WHERE CUR_STA = 1';
        $result = $this->db->fetchAll($query);
        $this->view->turmas = $result;
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            
            $arr = array(
                'MRC_DAT' => date('Y-m-d'),
                'MRC_HOR' => date('H:i:00'),
                'MRC_ASS' => $formData['assunto'],
                'MRC_TXT' => $formData['mensagem'],
                'USU_ID'  => Zend_Auth::getInstance()->getStorage()->read()->USU_ID,
                'ALU_ID'  => '',
                'CBS_ID'  => '',
                'CUR_ID'  => $formData['turma'],
                'MRC_STA' => '1',
            );
            $this->db->insert('MURAL_RECADOS',$arr);
            $id = $this->db->lastInsertId();
            
            $query = 'SELECT SQL_CACHE DISTINCT m.ALU_ID '
                    . 'FROM MATRICULA m, CURSO c, CURSOBASE cb '
                    . 'WHERE m.CUR_ID = c.CUR_ID AND cb.CBS_ID = c.CBS_ID '
                    . 'AND c.CUR_STA = 1 AND c.CUR_ID = '.$formData['turma'].'';
            $result = $this->db->fetchAll($query);
                                
            foreach($result as $alu){
                
                $query = 'SELECT SQL_CACHE USU_ID FROM ALUNO WHERE ALU_ID = '.$alu['ALU_ID'];
                $row = $this->db->fetchRow($query);

                $arr2 = array(
                    'MRC_ID'  => $id,
                    'USU_ID'  => $row['USU_ID'],
                    'ALU_ID'  => $alu['ALU_ID'],
                    'MRM_DAT' => $arr['MRC_DAT'],
                    'MRM_HOR' => $arr['MRC_HOR'],
                    'MRM_ASS' => $arr['MRC_ASS'],
                    'MRM_TXT' => $arr['MRC_TXT'],
                    'MRM_STA' => '1',
                );

                $this->db->insert('MURAL_RECADOS_MSG',$arr2);
            }
            echo '<script>alert("Mensagem enviada com sucesso!");</script>';
        }
    }
    
    
}